<?php

namespace App\Common\Libs\Ali;

use App\Common\Libs\Json;
use App\Consts\ExceptionCode;
use DingNotice\DingTalk;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
 * 钉钉机器人消息
 * Class DingReport
 * @package App\Common\Libs\Ali
 * @example $res = DingReport::getInstance()->text('测试消息');
 */
class DingReport
{

    /** @var array $config */
    protected $config;

    /** @var DingTalk $client */
    public $client;

    /** @var $prefix */
    public $prefix;

    /** @var $instance */
    public static $instance;

    /**
     * DingReport constructor.
     * @param array $config
     */
    public function __construct($config = [])
    {
        $config = $config ? : config('ding');
        $this->config = $config;
        $this->prefix = $config['default']['prefix'] ?? '';
        $this->client = new DingTalk($this->config);
    }

    /**
     * @param array $config
     * @return DingReport
     */
    public static function getInstance($config = [])
    {
        if (self::$instance instanceof AliYunSms) {
            return self::$instance;
        }
        return self::$instance = new self($config);
    }

    /**
     * @return DingTalk
     */
    public function client()
    {
        return $this->client;
    }

    /**
     * 发送文本消息
     * @param $content
     * @return mixed
     */
    public function text($content)
    {
        $content = "{$this->prefix }".$content;
        $result = $this->client->text($content);
        return $this->_result($result);
    }

    /**
     * 发送 markdown 消息
     * @param $title
     * @param $markdown
     * @return mixed
     */
    public function markdown($title, $markdown)
    {
        $title = "{$this->prefix }".$title;
        $markdown = "### {$title}\r\n" . $markdown;
        $result = $this->client->markdown($title, $markdown);
        return $this->_result($result);
    }

    /**
     * 发送链接消息
     * @param $title
     * @param $text
     * @param $url
     * @param string $picUrl
     * @return mixed
     */
    public function link($title, $text, $url, $picUrl = '')
    {
        $title = "{$this->prefix }".$title;
        $result = $this->client->link($title, $text, $url, $picUrl);
        return $this->_result($result);
    }

    /**
     * @param $title
     * @param $text
     * @return mixed
     */
    public function action($title, $text)
    {
        $result = $this->client->action($title, $text);
        return $this->_result($result);
    }

    /**
     * @param array $mobiles
     * @param bool $atAll
     * @return $this
     */
    public function at($mobiles = [], $atAll = false)
    {
        $this->client = $this->client->at($mobiles, $atAll);
        return $this;
    }

    /**
     * @param string $robot
     * @return $this
     */
    public function with($robot = 'default'){
        $this->client = $this->client->with($robot);
        return $this;
    }

    /**
     * @param $title
     * @param $markdown
     * @param int $hideAvatar
     * @param int $btnOrientation
     * @return mixed
     */
    public function actionCard($title, $markdown, $hideAvatar = 0, $btnOrientation = 0)
    {
        $title = "{$this->prefix }".$title;
        $result = $this->client->actionCard($title, $markdown, $hideAvatar, $btnOrientation);
        return $this->_result($result);
    }

    /**
     * @return mixed
     */
    public function feed()
    {
        return $this->client->feed();
    }

    /**
     * @param $result
     * @return bool
     */
    private function _result($result)
    {
        if (isset($result['errmsg']) && $result['errmsg'] == 'ok') {
            return true;
        } else {
            throw new HttpException(ExceptionCode::CODE_FAIL, 'SEND ERROR : - ' . Json::encode($result));
        }
    }

}
